package yxy.game.pm2.command.yilingbattle;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.YiLingMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.yiling.YiLingBattleHandle;
import yxy.game.pm2.module.yiling.YiLingBattleModule;

import java.util.Collection;

/****************************************
 * @name CMD_YiLingBattleSweep
 * @description 夷陵之战 关卡扫荡
 * @author 梁健彬
 * @since 2022-05-17
 ***************************************/
@MessageType(proto = Command.YiLingBattleSweep, classtype = YiLingMessage.YiLingBattleSweepRequest.class)
public class CMD_YiLingBattleSweep extends Command<YiLingMessage.YiLingBattleSweepRequest> {

    @Override
    public void execute(MessageSource messageSource, YiLingMessage.YiLingBattleSweepRequest request) {
        String playerCode = messageSource.getTag();

        int mapId = request.getMapId();

        YiLingMessage.YiLingBattleSweepResponse.Builder response = YiLingMessage.YiLingBattleSweepResponse.newBuilder();

        YiLingBattleModule module = YiLingBattleModule.get(playerCode);

        try {
            if (module.tryLock()) {

                if (mapId > module.getMapId()) {
                    send(messageSource, response, Result.YiLingBattleCantSweepFloor);// 这层还没有挑战成功，不能扫荡
                    return;
                }

                if(module.remainTimes() <= 0){
                    send(messageSource, response, Result.YiLingBattleChanllengeTimesLack);// 没有挑战次数了
                    return;
                }

                if (module.getTimes() > 0){
                    Currency cost = Currency.create(CurrencyType.CT_元宝, 0, YiLingBattleModule.sweepCost);
                    try {
                        new CurrencyHandle().cost(playerCode, CCReason.夷陵之战, cost);
                    } catch (CurrencyNotEnoughException e) {
                        new Result(Result.CostFail, e.getType().getNumber(), e.getDataId());// 消费不足
                    }
                }

                //挑战次数+1
                module.setTimes(module.getTimes() + 1);
                module.set();

                //剩余扫荡次数
                response.setTimes(module.remainTimes());

                if (module.getTimes() > 0){
                    response.setIsCost(1);
                }else {
                    response.setIsCost(0); //首次免费
                }

                //扫荡奖励
                Collection<Currency> rewards = YiLingBattleHandle.sweepRewards(mapId);

                new CurrencyHandle().addCurrency(playerCode, CCReason.夷陵之战, rewards);

                response.addAllRewards(new CurrencyHandle().packItems(rewards));

                send(messageSource, response, Result.Success);
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            module.unlock();
        }

    }

    private void send(MessageSource messageSource, YiLingMessage.YiLingBattleSweepResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}